<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>表单页面</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../../../lib/layui/css/layui.css" media="all">
  <link href="../../../lib/wangeditor/style.css" rel="stylesheet">
  <link href="../../../css/layout.css" rel="stylesheet">
  <link rel="stylesheet" href="../../../css/public.css" media="all">
  <style>
      body {
          background-color: #ffffff;
      }
  </style>
</head>
<body>
<form class="layui-form" action="" lay-filter="demo-val-filter">
<div class="layui-form layuimini-form" id="myForm">
  <div class="layui-form-item">
    <label class="layui-form-label required">名称</label>
    <div class="layui-input-block">
      <input type="text" name="name" lay-verify="required" placeholder="请输入" value="" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">描述</label>
    <div class="layui-input-block">
      <textarea name="description" placeholder="请输入" class="layui-textarea"></textarea>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">地址</label>
    <div class="layui-input-block">
      <input type="text" name="href" lay-verify="required" placeholder="请输入" value="" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">是否显示</label>
    <div class="layui-input-block">
      <input type="checkbox" name="showFlag" lay-skin="switch" lay-filter="topFlag" lay-text="是|否">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">背景图片</label>
    <div class="layui-input-block">
      <button type="button" class="layui-btn" id="upload-btn">
        <i class="layui-icon layui-icon-upload"></i> 背景上传
      </button>
      <div style="width: 132px;">
        <div class="layui-upload-list">
          <img class="layui-upload-img" id="ID-upload-demo-img" style="width: 100%; height: 92px;">
          <div id="ID-upload-demo-text"></div>
        </div>
        <div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="filter-demo">
          <div class="layui-progress-bar" lay-percent=""></div>
        </div>
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">排序</label>
    <div class="layui-input-block">
      <input type="number" name="order" lay-verify="required" placeholder="请输入" value="" class="layui-input">
    </div>
  </div>

  <hr />

  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn" type="button">保存</button>
    </div>
  </div>
</div>
</form>
<script src="../../../lib/layui/layui.js" charset="utf-8"></script>
<script src="../../../lib/wangeditor/index.js"></script>
<script src="../../../api/common.js" charset="utf-8"></script>

<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        // 获取URL参数
        function getQueryString(name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURIComponent(r[2]);
            }
            return null;
        }

        // 获取页面ID
        var id = getQueryString('id');
        console.log(id);
        let fileId
        // 发起AJAX请求
        $.ajax({
            type: 'GET',
            url: local + 'topic/web/get/' + id,
            dataType: "json",
            headers: {
                'Authorization': `Bearer ${ localStorage.getItem("token") }`,
            },
            contentType: 'application/json;charset=UTF-8',
            success: function (res) {
                if (res.code === 200 && res.data) {
                    fileId = res.data.fileId
                    // 数据回显到表单
                    form.val('demo-val-filter', {
                        'name': res.data.name,
                        'description': res.data.description,
                        'href': res.data.href,
                        'showFlag': res.data.showFlag ? true : false,
                        'order': res.data.order,
                    });
                    // 照片回显
                    if (res.data.filePath) {
                        $('#ID-upload-demo-img').attr('src', res.data.filePath);  // 将旧图片的URL赋值给图片标签
                    }
                    form.render(); // 渲染表单
                } else {
                    layer.alert('获取数据失败', {icon: 5});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
            }
        });

        layui.use(['form', 'laydate'], function () {
            var form = layui.form,
                layer = layui.layer,
                laydate = layui.laydate;
            var upload = layui.upload;
            var element = layui.element;
            // var fileId = '';

            // 上传组件
            var uploadInst = upload.render({
                elem: '#upload-btn',
                url: local + 'attachment/upload',
                data: {
                    objType: 1,
                },
                headers: {
                    'Authorization': `Bearer ${localStorage.getItem("token")}`,
                },
                before: function (obj) {
                    obj.preview(function (index, file, result) {
                        $('#ID-upload-demo-img').attr('src', result); // 显示上传的图片
                    });
                    element.progress('filter-demo', '0%');
                    layer.msg('上传中', {icon: 16, time: 0});
                },
                done: function (res) {
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    if (res.errno == 0) {
                        fileId = res.data.attrId;
                    }
                    $('#ID-upload-demo-text').html('');
                },
                error: function () {
                    var demoText = $('#ID-upload-demo-text');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                },
                progress: function (n, elem, e) {
                    element.progress('filter-demo', n + '%');
                    if (n == 100) {
                        layer.msg('上传完毕', {icon: 1});
                    }
                }
            });

            // 监听表单提交
            form.on('submit(saveBtn)', function (data) {
                var obj = data.field;
                obj.fileId = fileId;
                obj.id = id;
                // showFlag处理
                if (obj.showFlag) {
                    obj.showFlag = true;
                } else {
                    obj.showFlag = false;
                }

                $.ajax({
                    type: 'POST',
                    url: local + 'topic/web/update',
                    data: JSON.stringify(obj),
                    dataType: "json",
                    headers: {
                        'Authorization': `Bearer ${localStorage.getItem("token")}`,
                    },
                    contentType: 'application/json;charset=UTF-8',
                    success: function (res) {
                        if (res.code === 200) {
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                            parent.layui.table.reload('currentTableId');
                            return false;
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
                    }
                });
            });
        });
    });

</script>
</body>
</html>
